package com.example.ai.controller;

import com.example.ai.treeService.TreeService;
import com.example.common.domain.Result;
import dev.langchain4j.community.store.embedding.redis.RedisEmbeddingStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.document.parser.apache.poi.ApachePoiDocumentParser;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiImageModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;


import java.util.List;

@Tag(name = "AI接口相关")
@Slf4j
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class AIController {

    private final TreeService treeService;

    private final EmbeddingModel embeddingModel;

    private final RedisEmbeddingStore redisEmbeddingStore;



    /**
     * @param message 浏览器传递的用户问题
     * @return 流式响应
     */
    @Operation(summary = "对话接口")
    @RequestMapping(value = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chat(String memoryId, String message) {
        log.info("用户memoryId:{},用户问题：{}", memoryId, message);
        Flux<String> result = treeService.chat(memoryId, message);

        System.out.println(result);
        return result;
    }

/*
public Flux<String> image(String memoryId, String message, @RequestParam(required = false)MultipartFile fles) {

    ChatLanguageModel  model = OpenAiChatModel.builder()
            .apiKey(System.getenv("OPENAI_API_KEY")) // Please use your own OpenAI API key
            .modelName("")
            .build();


    UserMessage userMessage = UserMessage.from(
            TextContent.from("What do you see?"),
            ImageContent.from("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")
    );
    Response<AiMessage> response = model.generate(userMessage);
    return treeService.chat(memoryId, message);
}
*/




    @Operation(summary = "刷新向量接口")
    @PostMapping("/refresh-vectors")
    public Result<String> refresh() {
        log.info("开始刷新向量数据");
        // 1. 加载文档进内存,并指定文档解析器
        List<Document> documents = ClassPathDocumentLoader.loadDocuments("content", new ApachePoiDocumentParser());
        // 2. 构建内存型向量数据库
        // InMemoryEmbeddingStore embeddingStore = new InMemoryEmbeddingStore();
        // 3. 构建一个EnbeddingIngestor 对象, 完成文本数据切割，向量化，存储
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(redisEmbeddingStore) // 配置向量数据存储的位置
                .embeddingModel(embeddingModel) // 配置向量化模型对象
                .build();
        ingestor.ingest(documents); // 切割文档，向量化，存储
        return Result.success("数据更新完成");
    }
}
